<style>
    .kami-table {
        width: 100%;
        border-collapse: collapse;
        margin: 15px 0;
        font-size: 14px;
    }
    .kami-table th {
        background-color: #F5F5F5;
        padding: 10px 15px;
        text-align: left;
        border: 1px solid #DDD;
        font-weight: bold;
    }
    .kami-table td {
        padding: 8px 15px;
        border: 1px solid #DDD;
        vertical-align: middle;
    }
    .kami-table tr:nth-child(even) {
        background-color: #F9F9F9;
    }
    .kami-table tr:hover {
        background-color: #F0F7FF;
    }
</style>
<div style="display: flex;align-items: center;">
    <div style="margin:10px 0;">
        <div style="display:inline-block;margin-right:10px;">
            <select id="delimiterSelect">
                <option value="
">换行(↵)</option>
                <option value=",">逗号(,)</option>
                <option value="|">竖线(|)</option>
                <option value=";">分号(;)</option>
            </select>
            <button onclick="exportWithDelimiter()" style="margin-right:10px;">导出卡密</button>
        </div>
    </div>

    <!-- 搜索表单 -->
    <form name="cpform" method="post" autocomplete="off"  action="{$url_page}" method="get">
        <input type="text" name="kami_key" value="{$query['kami_key']}" placeholder="请输入卡密">
        <select name="kami_type">
            <option value="">全部积分类型</option>
            <!--{loop $kami_type_list $kami_type}-->
                <option value="{$kami_type['id']}" <!--{if $query['kami_type'] == $kami_type['id']}-->selected<!--{/if}-->>{$kami_type['name']}</option>
            <!--{/loop}-->
        </select>
        <select name="pagesize">
            <option value="10" {if $pagesize == 10}selected{/if}>10条/页</option>
            <option value="20" {if $pagesize == 20}selected{/if}>20条/页</option>
            <option value="50" {if $pagesize == 50}selected{/if}>50条/页</option>
            <option value="100" {if $pagesize == 100}selected{/if}>100条/页</option>
        </select>
        <!-- <input type="text" name="page" value="{$page}"> -->
        <input type="submit" value="搜索">
    </form>
    <div>
        <a href="plugin.php?id=erling_kami_buy:user_kami_exchange" style="margin-left: 15px;">获取卡密</a>
    </div>
</div>
<!-- 列表 -->
<table class="kami-table">
    <tr>
        <th width="10%">卡密（点击显示）</th>
        <th width="10%">分类</th>
        <th width="10%">消耗积分类型</th>
        <th width="10%">消耗积分</th>
    </tr>
    <!--{loop $kami_list $kami}-->
    <tr>
        <td style="word-break: break-all;">
            <input type="password" 
               value="{$kami['kami_key']}" 
               readonly 
               style="border:none;background:transparent;width:150px;cursor:pointer;"
               onclick="togglePasswordVisibility(this)">
        </td>
        <td>{$kami_type['kami_type']}
            <!--{loop $kami_type_list $kami_type}-->
                <!--{if $kami_type['id'] == $kami['kami_type']}-->
                    {$kami_type['name']}
                <!--{/if}-->
            <!--{/loop}-->
        </td>
        <td>{$extcredits_data[$kami['extcredits']]['title']}</td>
        <td>{$kami['credit']}</td>
    </tr>
    <!--{/loop}-->
</table>
<!-- 分页 -->
{$multipage}
<div style="padding: 20px;"></div>
<script>
function exportWithDelimiter() {
    // 获取选择的分隔符
    var delimiter = document.getElementById('delimiterSelect').value;
    
    // 获取所有卡密
    var kamis = [];
    var rows = document.querySelectorAll('.kami-table tr:not(:first-child)');
    rows.forEach(function(row) {
        var input = row.querySelector('input[type="password"], input[type="text"]');
        if(input) {
            kamis.push(input.value.trim());
        }
    });
    
    if(kamis.length === 0) {
        alert('没有可导出的卡密');
        return;
    }
    
    // 创建内容，使用选择的分隔符
    var content = kamis.join(delimiter === '\n' ? '\r\n' : delimiter);
    
    // 创建下载方式
    var file = new File([content], '卡密列表_' + new Date().toLocaleDateString() + '.txt', {
        type: 'text/plain;charset=utf-8'
    });
    
    // 创建下载链接
    var url = URL.createObjectURL(file);
    try {
        var a = document.createElement('a');
        a.href = url;
        a.download = file.name;
        document.body.appendChild(a);
        a.click();
    } finally {
        setTimeout(function() {
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
        }, 100);
    }
}

function togglePasswordVisibility(input) {
    input.type = input.type === 'password' ? 'text' : 'password';
}
</script>

